/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import type.InvoiceType;

/**
 *
 * @author wei
 */
@Entity(name = "BillingRequest")
public class BillingRequestEntity extends DataObjectEntity implements Serializable {

    private double requestedAmount;
    @OneToOne
    private SalesOrderEntity salesOrder;
    @OneToMany(mappedBy = "billingRequest")
    private Collection<ShippingOrderEntity> shippingOrders = new ArrayList<ShippingOrderEntity>();
    @OneToOne(mappedBy = "billingRequest")
    private CustomerInvoiceEntity invoice;

    public BillingRequestEntity() {
        super();
    }

    public BillingRequestEntity(double requestedAmount, SalesOrderEntity salesOrder, Collection<ShippingOrderEntity> shippingOrder) {
        this();
        this.requestedAmount = requestedAmount;
        this.salesOrder = salesOrder;
        this.shippingOrders = shippingOrder;
    }

    public double getRequestedAmount() {
        return requestedAmount;
    }

    public void setRequestedAmount(double requestedAmount) {
        this.requestedAmount = requestedAmount;
    }

    public SalesOrderEntity getSalesOrder() {
        return salesOrder;
    }

    public void setSalesOrder(SalesOrderEntity salesOrder) {
        this.salesOrder = salesOrder;
    }

    public Collection<ShippingOrderEntity> getShippingOrders() {
        return shippingOrders;
    }

    public void setShippingOrders(Collection<ShippingOrderEntity> shippingOrders) {
        this.shippingOrders = shippingOrders;
    }

    public CustomerInvoiceEntity getInvoice() {
        return invoice;
    }

    public void setInvoice(CustomerInvoiceEntity invoice) {
        this.invoice = invoice;
    }

    public InvoiceType getType() {
        if (shippingOrders.isEmpty()) {
            return InvoiceType.cashInAdvance;
        } else {
            return InvoiceType.normal;
        }
    }

    public boolean hasInvoice() {
        return invoice != null;
    }

    public List<ProductQuantityPriceEntity> calculatePQPs() {
        List<ProductQuantityPriceEntity> pqps = new ArrayList<ProductQuantityPriceEntity>();
        HashMap<ProductEntity, Long> productToQuantity = new HashMap<ProductEntity, Long>();
        for (ShippingOrderEntity shipping : shippingOrders) {
            Collection<ShippingRequestEntity> requests = shipping.getPackingOrder().getProductToQuantity();
            for (ShippingRequestEntity request : requests) {
                Long quantity = productToQuantity.get(request.getProduct());
                if (quantity == null) {
                    quantity = 0L;
                }
                productToQuantity.put(request.getProduct(), quantity + request.getQuantity());
            }
        }
        
        HashMap<ProductEntity, Double> productToUnitPrice = salesOrder.getPurchaseOrder().getProductToUnitPriceHash();
        for(ProductEntity p : productToQuantity.keySet()){
            ProductQuantityPriceEntity pqp = new ProductQuantityPriceEntity();
            pqp.setProduct(p);
            pqp.setQuantity(productToQuantity.get(p));
            
            Double unitPrice = productToUnitPrice.get(p);
            if(unitPrice==null)
                unitPrice = 0.0;
            pqp.setPrice(unitPrice*pqp.getQuantity());
            pqps.add(pqp);
        }

        return pqps;
    }

    @Override
    public String childToString() {
        return "BillingRequestEntity{" + "requestedAmount=" + requestedAmount + ", salesOrder=" + salesOrder + ", shippingOrder=" + shippingOrders + ", invoice=" + invoice + '}';
    }
}
